import pygame
import sys
import random

class Snake:
    def __init__(self):
        self.long = 5
        self.direction = 0
        self.body = []
        self.square = 18
        for i in range(self.long):
            self.addnode()

    def addnode(self):
        if self.body:
            top = self.body[0].gettop()
            left = self.body[0].getleft()
            # left
            if self.direction == 0:
                newpoint = Point(left-self.square, top)
            # right
            elif self.direction == 1:
                newpoint = Point(left+self.square, top)
            # top
            elif self.direction == 2:
                newpoint = Point(left, top-self.square)
            # bottom
            elif self.direction == 3:
                newpoint = Point(left, top+self.square)
            self.body.insert(0, newpoint)
        else:
            newpoint = Point(random.randint(0, width)//self.square*self.square,
                             random.randint(0, height)//self.square*self.square)
            self.body.insert(0, newpoint)

    def delnode(self):
        self.body.pop()

    def iscollsion(self):
        for point in self.body[1:]:
            if self.body[0].left == point.left and self.body[0].top == point.top:
                return True
        return False



class Point:
    def __init__(self, left, top):
        self.left = left
        self.top = top


    def getleft(self):
        return self.left

    def gettop(self):
        return self.top

def random_point(snake):
    newpoint = Point(random.randint(0, width)//snake.square*snake.square,
                     random.randint(0, height)//snake.square*snake.square)
    for point in snake.body:
        if newpoint.top == point.top and newpoint.left == point.left:
            return random_point(snake)
    return newpoint


pygame.init()  # 初始化pygame
size = width, height = 640, 480  # 设置窗口大小
color = (0, 0, 0)  # 设置颜色
clock = pygame.time.Clock()  # 设置时钟
snake = Snake()
food = random_point(snake)
isAlive = True

while isAlive:  # 死循环确保窗口一直显示
    clock.tick(5)  # 每秒执行60次
    for event in pygame.event.get():  # 遍历所有事件
        if event.type == pygame.QUIT:  # 如果单击关闭窗口，则退出
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                if snake.direction != 1:
                    snake.direction = 0

            if event.key == pygame.K_RIGHT:
                if snake.direction != 0:
                    snake.direction = 1

            if event.key == pygame.K_UP:
                if snake.direction != 3:
                    snake.direction = 2

            if event.key == pygame.K_DOWN:
                if snake.direction != 2:
                    snake.direction = 3

    screen = pygame.display.set_mode(size)  # 显示窗口
    pygame.draw.rect(screen, (255, 255, 0), (food.left, food.top, snake.square, snake.square))
    if food.left == snake.body[0].left and food.top == snake.body[0].top:
        snake.addnode()
        food = random_point(snake)
    snake.addnode()
    snake.delnode()
    if snake.body[0].left < 0 or snake.body[0].left > width or snake.body[0].top < 0 or snake.body[0].top > height:
        print("Game Over!")
        isAlive = False
    if snake.iscollsion():
        print("Collision!")
        isAlive = False

    for point in snake.body:
        pygame.draw.rect(screen, (245, 135, 155), (point.left, point.top, snake.square, snake.square))
    pygame.display.flip()  # 更新全部显示



pygame.quit()  # 退出pygame
